/*
	       File: Workflow.cs
	Create Date: 2007-08-16
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;

using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public partial class ucWorkFlow : UserControl
    {
        #region Private member fields
        private Configuration _Configuration;
        private ConfigurationWorkflow _configWorkFlow;
        #endregion

        #region Constructors and cleanup methods
        public ucWorkFlow()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        private void OnFinishedUpdate()
        {
            if (null != FinishedUpdate)
            {
                FinishedUpdate(_configWorkFlow.Name, _configWorkFlow.ID);
            }
        }
        #endregion

        #region Class properties
        #endregion

        #region Events handlers
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _configWorkFlow.Name = txtName.Text;
            _configWorkFlow.Description = txtDescription.Text;
            _configWorkFlow.WorkflowStrongName = txtStrongName.Text;
            OnFinishedUpdate();
        }
        #endregion

        #region Public interface
        public void LoadWorkflow(Configuration configuration, ConfigurationWorkflow workFlow)
        {
            if (null != configuration && null != workFlow)
            {
                _Configuration = configuration;
                _configWorkFlow = workFlow;

                txtName.Text = _configWorkFlow.Name;
                txtDescription.Text = _configWorkFlow.Description;
                txtStrongName.Text = _configWorkFlow.WorkflowStrongName;
            }
        }
        public delegate void FinishedUpdateDelegate(string name, string id);
        public event FinishedUpdateDelegate FinishedUpdate;
        #endregion

        private void btnSelectWorkflow_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = _Configuration.Services.AssemblyDirectory;
            ofd.Filter = "assembly files (*.dll)|*.dll|All files (*.*)|*.*";
            ofd.FileName = txtName.Text + ".dll";

            if (DialogResult.OK == ofd.ShowDialog())
            {
                Assembly assembly = Assembly.LoadFile(ofd.FileName);
                List<Type> workflows = new List<Type>();
                Type[] assemblyTypes = assembly.GetTypes();

                //First find all of the workflows we are interested in
                foreach (Type asmType in assemblyTypes)
                {
                    if (true == asmType.IsSubclassOf(typeof(SequentialWorkflowActivity)))
                    {
                        workflows.Add(asmType);
                    }
                }

                //Now display the list and ask the user which one
                dlgSelectWorkflow selectWorkflow = new dlgSelectWorkflow(workflows);
                if (DialogResult.OK == selectWorkflow.ShowDialog())
                {
                    txtStrongName.Text = string.Format("{0},{1}",selectWorkflow.SelectedWorkflow, assembly.FullName);
                }
            }
        }
    }
}
